package com.app.megaprosoft.DB;

import java.sql.*;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class Province {
    ResultSet rs = null;
	
    SQLiteDBClass myDb;
    SQLiteDatabase db;
    Context context;
	private static final String TABLE_Addresses = "_Addresses";
	
	public ResultSet selectProvince() throws Exception {
		Connect con = new Connect(); 
		String StrSql   = "SELECT [AddressCode]"+
				                ",[ProvinceName]"+		
				                ",[Type]"+		
				                ",[ProvinceCode] "+								 					 
				          "FROM [dbo].[udfProvince] () ";
		
			StrSql += "ORDER BY [ProvinceName] ASC ";
    	
			return con.selectData(StrSql);
	 }
//	 public void closeConnecttion() throws Exception {
//    	 if (conn != null) {
//             conn.close();
//         }
//    }
	 
		public void DEFINE_Addresses() {
			rs = null;
			
			myDb = new SQLiteDBClass(getContext());
			db = myDb.getWritableDatabase();

			try {
				rs = selectProvince();
				while (rs.next()) {
					ContentValues Val = new ContentValues();
					Val.put("AddressesOID", rs.getString("AddressCode"));
					Val.put("ProvinceName", rs.getString("ProvinceName"));
					Val.put("Type", rs.getString("Type"));
					Val.put("ProvinceCode", rs.getString("ProvinceCode"));
					// Val.put("IsFavorite",rs.getString("IsFavorite"));
					// Favorite Province
					if (rs.getString("ProvinceCode").equals("50")) {
						Val.put("IsFavorite", "1");
					} else if (rs.getString("ProvinceCode").equals("10")) {
						Val.put("IsFavorite", "2");
					} else if (rs.getString("ProvinceCode").equals("51")) {
						Val.put("IsFavorite", "3");
					} else if (rs.getString("ProvinceCode").equals("52")) {
						Val.put("IsFavorite", "4");
					} else if (rs.getString("ProvinceCode").equals("57")) {
						Val.put("IsFavorite", "5");
					} else {
						Val.put("IsFavorite", "0");
					}
					db.insert(TABLE_Addresses, null, Val);
				}
				rs.close();
			} catch (Exception e) {
				Log.d("TABLE_Addresses", "Create Table not Successfully.");
			}
			Log.d("TABLE_Addresses", "Create Table Successfully.");
			db.close();
		}
		public Context getContext() {
			return context;
		}
		public void setContext(Context context) {
			this.context = context;
		}
		
		public String[][] SelectAll_Addresses(String StrOption) throws Exception{
			// TODO Auto-generated method stub
			try {
				String arrData[][] = null;
				
				myDb = new SQLiteDBClass(getContext());
				db = myDb.getWritableDatabase();
				
				String strSQL = "SELECT  AddressesOID As _id , * FROM "
						+ TABLE_Addresses ;
				if (StrOption.equals("0")) {
					strSQL += " WHERE [IsFavorite] LIKE '" + StrOption + "' ";
				}else if (StrOption.equals("1")){
					strSQL += " WHERE [IsFavorite] NOT LIKE '0' ORDER BY IsFavorite ASC";
				}
				Cursor cursor = db.rawQuery(strSQL, null);
				if (cursor != null) {
					if (cursor.moveToFirst()) {
						arrData = new String[cursor.getCount()][cursor
								.getColumnCount()];
						int i = 0;
						do {
							arrData[i][0] = cursor.getString(1);
							arrData[i][1] = cursor.getString(2);
							arrData[i][2] = cursor.getString(3);
							arrData[i][3] = cursor.getString(4);
							arrData[i][4] = cursor.getString(5);
							i++;
						} while (cursor.moveToNext());
					}
				}
				cursor.close();
				return arrData;
			} catch (Exception e) {
				return null;
			}
		}
		
}
